<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Request\SendConfigRequest;
use App\CustHub\Service\ChSendConfigService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;

/**
 * 发送配置
 */
#[Controller(prefix: 'ch/send/config'),VerifySignature,ApiAuth]
class SendConfigController extends BaseController
{

    #[Inject]
    protected ChSendConfigService $service;

    /**
     * 配置列表
     * @param SendConfigRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('list')]
    public function list(SendConfigRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success('获取成功', $this->service->getConfigList($request->all()));
    }

    /**
     * 获取配置信息
     * @param SendConfigRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getInfo')]
    public function getInfo(SendConfigRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $mainUid = $request->input('main_uid');
        $type = $request->input('type');
        $robotId = $request->input('robot_id');
        return $this->success('获取成功',$this->service->getConfig($type,$mainUid,$robotId));
    }

    /**
     * 保存配置
     * @param SendConfigRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('save')]
    public function save(SendConfigRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $mainUid = $request->input('main_uid');
        $uid = $request->input('uid');
        $type = $request->input('type');
        $corp_id = $request->input('corp_id');
        $robot_ids = $request->input('robot_ids');
        $value = $request->input('value');
        $robot_ids = !is_array($robot_ids) ? json_decode($robot_ids, true) : $robot_ids;
        foreach ($robot_ids as $robot_id) {
            $this->service->saveConfig($type, $uid, $mainUid, $corp_id, $robot_id, $value);
        }

        return $this->success('保存成功');
    }
}